package com.dcrzl.shcz.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcrzl.shcz.common.entity.sys.SysFile;
import com.dcrzl.shcz.common.mapper.sys.SysFileMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.common.utils.SysUtils;
import com.dcrzl.shcz.module.sys.service.SysFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("sysFileService")
public class SysFileServiceImpl extends BaseServiceImpl<SysFileMapper, SysFile> implements SysFileService {

    @Value("${my.file.path}")
    private String baseFilePath;

    @Override
    public Result<SysFile> selectOne(SysFile sysFile) {
        LambdaQueryWrapper<SysFile> wrapper = new LambdaQueryWrapper<>();
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<SysFile>> selectList(SysFile sysFile) {
        LambdaQueryWrapper<SysFile> wrapper = new LambdaQueryWrapper<>();
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<SysFile> selectPage(SysFile sysFile) {
        LambdaQueryWrapper<SysFile> wrapper = new LambdaQueryWrapper<>();
        return super.selectPage(sysFile, wrapper);
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        SysFile sysFile = new SysFile().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(sysFile);
    }

    @Override
    public Result<SysFile> uploadFile(List<MultipartFile> files, String bucket) {
        if (CollectionUtils.isEmpty(files)) {
            return new Result().setSuccess(false).setCode(SysConstant.PARAM_LACK_CODE_RES).setMsg("上传文件不能为空" );
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String filePath = sdf.format(new Date());
        Result resultEntity = new Result();
        List<SysFile> sysfileList = new ArrayList<>();
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            String obsFileName = SysUtils.getUUID();
            String filename = file.getOriginalFilename();
            String suffix = file.getOriginalFilename().substring(filename.lastIndexOf("." ));
            if (StringUtils.hasText(suffix)) {
                obsFileName = obsFileName + suffix;
            }
            //new File(filePath).getAbsolutePath()必须获取绝对路径
            File filePaths = new File(baseFilePath+"/"+filePath);
            if(!filePaths.exists()){
                boolean mkdirs = filePaths.mkdirs();
            }
            File dest = new File(filePaths.getAbsolutePath() , obsFileName);
            try {
                file.transferTo(dest);
                SysFile files1 = new SysFile().setType(file.getContentType());
                files1.setFileName(file.getOriginalFilename()).setPath(filePath+"/"+obsFileName);
                try {
                    this.insert(files1);
                    sysfileList.add(files1);
                }catch (Exception e){
                    e.printStackTrace();
                    return resultEntity.setSuccess(false).setMsg("数据库保存第" + (i++) + "个文件失败");
                }
            } catch (IOException e) {
                e.printStackTrace();
                return resultEntity.setSuccess(false).setMsg("上传第" + (i++) + "个文件失败");
            }
        }

        return  resultEntity.setSuccess(true).setMsg("上传成功").setData(sysfileList);
    }


}